using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PluginPathFinder;

public class Enemy : MonoBehaviour
{
    private Vector3 destPosition = new Vector3(91f,-10f,64f);

    public float moveSpeed = 10.0f;

    public delegate void DieEvent(GameObject gameobject);

    public DieEvent OnDie;

    public float HP = 100f;
    public float MaxHp = 100;

    // Start is called before the first frame update
    void Start()
    { 
        StartCoroutine(StartMove());
        BuilderManager.Instance.AddEnemy(gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    
    IEnumerator StartMove()
    {
        
        yield return new WaitForSeconds(3);

        PathFinder.instance.FindShortestPathOfPoints(transform.position, destPosition, PathLineType.Straight,
             delegate (List<Vector3> points)
             {
                 PathFinderUtility.Stop(transform);
                 FollowThePathWithGroundSnap(points);
             });
    }

    void FollowThePathWithGroundSnap(List<Vector3> nodes)
    {
        PathFinderUtility.FollowPathWithGroundSnap(transform, nodes, moveSpeed, true, Vector3.down, 0.0f, LayerMask.NameToLayer(PathFinder.instance.graphData.groundColliderLayerName));
    }

    public void TakeDamage(float damaage)
    {
        if(HP > damaage)
        {
            HP = HP - damaage;
        } 
        else
        {
            HP = 0.0f;
          
        }
        gameObject.GetComponent<HeadIcon>().SetHP((int)HP);
        if(HP < 0.1f)
        {
            Die();
        }
    }

    public void Die()
    {
        BuilderManager.Instance.RemoveEnemy(gameObject);
        StopAllCoroutines();
        OnDie(gameObject);
        Destroy(gameObject);
    }
}
